package com.ruoyi.datastatistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.datastatistics.domain.TerminalsMonfreezeDb;
import com.ruoyi.datastatistics.mapper.TerminalsMonfreezeDbMapper;
import com.ruoyi.datastatistics.service.ITerminalsMonfreezeDbService;
import com.ruoyi.system.domain.SysSheetUsers;
import com.ruoyi.system.mapper.SysSheetUsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 大表月用量数据Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-07
 */
@Service
public class TerminalsMonfreezeDbServiceImpl implements ITerminalsMonfreezeDbService {
    @Autowired
    private TerminalsMonfreezeDbMapper terminalsMonfreezeDbMapper;
    @Autowired
    private SysSheetUsersMapper sysSheetUsersMapper;

    /**
     * 查询大表月用量数据
     *
     * @param Id 大表月用量数据主键
     * @return 大表月用量数据
     */
    @Override
    public TerminalsMonfreezeDb selectTerminalsMonfreezeDbById(Long Id) {
        return terminalsMonfreezeDbMapper.selectTerminalsMonfreezeDbById(Id);
    }

    /**
     * 查询大表月用量数据列表
     *
     * @param terminalsMonfreezeDb 大表月用量数据
     * @return 大表月用量数据
     */
    @Override
    public List<TerminalsMonfreezeDb> selectTerminalsMonfreezeDbList(Map<String, Object> paramMap) {
        return terminalsMonfreezeDbMapper.selectTerminalsMonfreezeDbList(paramMap);
    }

    @Override
    public List<TerminalsMonfreezeDb> selectTerminalsMonfreezeDbLists(String TerminalID, String startTime,
                                                                      String endTime) {
        return terminalsMonfreezeDbMapper.selectTerminalsMonfreezeDbLists(TerminalID, startTime, endTime);
    }

    /**
     * 新增大表月用量数据
     *
     * @param terminalsMonfreezeDb 大表月用量数据
     * @return 结果
     */
    @Override
    public int insertTerminalsMonfreezeDb(TerminalsMonfreezeDb terminalsMonfreezeDb) {
        return terminalsMonfreezeDbMapper.insertTerminalsMonfreezeDb(terminalsMonfreezeDb);
    }

    /**
     * 修改大表月用量数据
     *
     * @param terminalsMonfreezeDb 大表月用量数据
     * @return 结果
     */
    @Override
    public int updateTerminalsMonfreezeDb(TerminalsMonfreezeDb terminalsMonfreezeDb) {
        return terminalsMonfreezeDbMapper.updateTerminalsMonfreezeDb(terminalsMonfreezeDb);
    }

    /**
     * 批量删除大表月用量数据
     *
     * @param Ids 需要删除的大表月用量数据主键
     * @return 结果
     */
    @Override
    public int deleteTerminalsMonfreezeDbByIds(Long[] Ids) {
        return terminalsMonfreezeDbMapper.deleteTerminalsMonfreezeDbByIds(Ids);
    }

    /**
     * 删除大表月用量数据信息
     *
     * @param Id 大表月用量数据主键
     * @return 结果
     */
    @Override
    public int deleteTerminalsMonfreezeDbById(Long Id) {
        return terminalsMonfreezeDbMapper.deleteTerminalsMonfreezeDbById(Id);
    }

    @Override
    public List<TerminalsMonfreezeDb> reportListByMonth(TerminalsMonfreezeDb terminalsMonfreezeDb) {
        Long sysUserId = (Long) terminalsMonfreezeDb.getParams().get("sysUserId");
        Boolean export = (Boolean) terminalsMonfreezeDb.getParams().get("export");
        if (sysUserId != null){
            List<SysSheetUsers> sheetUsersList = sysSheetUsersMapper.selectList(new LambdaQueryWrapper<SysSheetUsers>().eq(SysSheetUsers::getSysUserid, sysUserId.intValue()));
            Set<Integer> sheetIds = sheetUsersList.stream().map(SysSheetUsers::getSheetid).collect(Collectors.toSet());
            terminalsMonfreezeDb.setSheetId(StringUtils.join(sheetIds,","));
        }
        if (export == null || !export) {
            PageUtils.startPage();
        }
        return terminalsMonfreezeDbMapper.reportListByMonth(terminalsMonfreezeDb);
    }

    @Override
    public List<TerminalsMonfreezeDb> reportListByYear(TerminalsMonfreezeDb terminalsMonfreezeDb) {
        Long sysUserId = (Long) terminalsMonfreezeDb.getParams().get("sysUserId");
        Boolean export = (Boolean) terminalsMonfreezeDb.getParams().get("export");
        if (sysUserId != null){
            List<SysSheetUsers> sheetUsersList = sysSheetUsersMapper.selectList(new LambdaQueryWrapper<SysSheetUsers>().eq(SysSheetUsers::getSysUserid, sysUserId.intValue()));
            Set<Integer> sheetIds = sheetUsersList.stream().map(SysSheetUsers::getSheetid).collect(Collectors.toSet());
            terminalsMonfreezeDb.setSheetId(StringUtils.join(sheetIds,","));
        }
        if (export == null || !export) {
            PageUtils.startPage();
        }
        return terminalsMonfreezeDbMapper.reportListByYear(terminalsMonfreezeDb);
    }
}
